﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Entity;
using PM.Services;

namespace PM.Site.Admin
{
    public partial class RegisterBonusRightAnnouncement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

        }

        private void Loading()
        {

            if (!Page.IsPostBack)
            {
                dlBonus.DataSource = DividendServices.GetFutureBonusIssueAnnounceCollection();
                dlBonus.DataBind();
                
                dlRightsIssues.DataSource = DividendServices.GetFutureRightsIssueAnnounceCollection();
                dlRightsIssues.DataBind();              

            }
        }

        protected void btnBonusIssueSave_Click(object sender, EventArgs e)
        {
            Dividend dividend = new Dividend();

            dividend.CompanyId = txbBonusCompId.Text;
            dividend.Name = txbBonusCompName.Text;
            DateTime announced = DateTime.MinValue;
            if (DateTime.TryParse(txbBonusAnnuDate.Text, out announced))
            {
                dividend.AnnouncedDate = announced;
            }
            decimal propotion = 0.00m;
            if (decimal.TryParse(txbBonusPropostion.Text, out propotion))
            {
                dividend.Propotion = propotion;
            }
            decimal shareValue = 0.00m;
            if (decimal.TryParse(txbBonusIssueValue.Text, out shareValue))
            {
                dividend.ValuePerShare = shareValue;
            }
            Int64 noOfnewShares = 0;
            Int64.TryParse(txbBonusNoOfNewShares.Text, out noOfnewShares);
            dividend.NoOfNewSharesListed = noOfnewShares;

            DateTime xd = DateTime.MinValue;
            if (DateTime.TryParse(txbBonusXD.Text, out xd))
            {
                dividend.XD = xd;
            }
            DateTime paymentDate = DateTime.MinValue;
            if (DateTime.TryParse(txbBonusIssueDate.Text, out paymentDate))
            {
                dividend.Payment = paymentDate;
            }

            dividend.Remarks = txbBonusRemarks.Text;
            DividendServices.Add(dividend);
            Response.Redirect("~/Admin/RegisterBonusRightAnnouncement.aspx");

        }

        protected void btnRightsIssueSave_Click(object sender, EventArgs e)
        {
            Dividend dividend = new Dividend();
            dividend.IsRightsIssue = true;
            dividend.CompanyId = txbRightCompId.Text;
            dividend.Name = txbRightCompName.Text;
            DateTime announced = DateTime.MinValue;            
            if (DateTime.TryParse(txbRightAnnuDate.Text, out announced))
            {
                dividend.AnnouncedDate = announced;
            }
            decimal propotion = 0.00m;
            if (decimal.TryParse(txbRightPropotion.Text, out propotion))
            {
                dividend.Propotion = propotion;
            }
            decimal shareValue = 0.00m;
            if (decimal.TryParse(txbRightShareValue.Text, out shareValue))
            {
                dividend.ValuePerShare = shareValue;
            }

            DateTime xd = DateTime.MinValue;
            if (DateTime.TryParse(txbRightXD.Text, out xd))
            {
                dividend.XD = xd;
            }

            DateTime renun = DateTime.MinValue;
            if (DateTime.TryParse(txbRightRenunciationDate.Text, out renun))
            {
                dividend.RenunciationDate = renun;
            }

            DateTime commencement = DateTime.MinValue;
            if (DateTime.TryParse(txbRightCommenceDate.Text, out commencement))
            {
                dividend.TradeCommencementDate = commencement;
            }
            
            DateTime paymentDate = DateTime.MinValue;
            if (DateTime.TryParse(txbRightAcceptanceDate.Text, out paymentDate))
            {
                dividend.Payment = paymentDate;
            }

            dividend.Remarks = txbRightRemarks.Text;
            DividendServices.Add(dividend);
            Response.Redirect("~/Admin/RegisterBonusRightAnnouncement.aspx");

        }
    }
}
